package com.atguigu.day03;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName BlockingQueueDemo
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-09-08 18:52
 * @Version 1.0
 */
public class BlockingQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        //1.抛出异常
        //System.out.println(blockingQueue.remove());
        /*System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        System.out.println(blockingQueue.add("d"));*/

        //2.特殊值
       /* System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d"));*/

        //3.阻塞
        /*new Thread(() -> {
            try {
                System.out.println("-----------000----");
                blockingQueue.take();
                blockingQueue.put("a");
                blockingQueue.put("a");
                blockingQueue.put("a");
                System.out.println("-----111-----");
                blockingQueue.put("x");
                System.out.println("-----222---");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();*/


        //4.超时
        new Thread(() -> {
            try {
                System.out.println("---000---");
                blockingQueue.poll(2,TimeUnit.SECONDS);
                blockingQueue.put("a");
                blockingQueue.put("b");
                blockingQueue.put("c");
                System.out.println("---111---");
                blockingQueue.offer("x",2,TimeUnit.SECONDS);
                System.out.println("---222---");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
    }
}
